package com.derbysoft.synxisadapter.dswitch3.translator;

import com.derbysoft.core.utils.DateTimeUtils;
import com.derbysoft.dswitch.dto.hotel.avail.RatePlanDTO;
import com.derbysoft.dswitch.dto.hotel.common.ChargeItemDTO;
import com.derbysoft.dswitch.dto.hotel.common.RateDTO;
import com.derbysoft.dswitch.dto.hotel.common.RoomRateDTO;

import java.math.BigDecimal;

/**
 * User: jason
 * Date: 2012-11-28
 */
public abstract class RateCalculator {

    public static BigDecimal calcAmountAfterTax(RoomRateDTO roomRateDTO) {
        BigDecimal totalAmountAfterTax = BigDecimal.ZERO;
        for (RateDTO rateDTO : roomRateDTO.getRatesList()) {
            int los = DateTimeUtils.calcLOS(rateDTO.getDateRange().getStart(), rateDTO.getDateRange().getEnd()) + 1;

            totalAmountAfterTax = totalAmountAfterTax.add(BigDecimal.valueOf(rateDTO.getAmountAfterTax()).multiply(BigDecimal.valueOf(los)));
        }
        return totalAmountAfterTax;
    }

    public static BigDecimal calcAmountBeforeTax(RoomRateDTO roomRateDTO) {
        BigDecimal totalAmountBeforeTax = BigDecimal.ZERO;
        for (RateDTO rateDTO : roomRateDTO.getRatesList()) {
            int los = DateTimeUtils.calcLOS(rateDTO.getDateRange().getStart(), rateDTO.getDateRange().getEnd()) + 1;

            totalAmountBeforeTax = totalAmountBeforeTax.add(BigDecimal.valueOf(rateDTO.getAmountBeforeTax()).multiply(BigDecimal.valueOf(los)));
        }
        return totalAmountBeforeTax;
    }

    public static BigDecimal calcTotalTax(RoomRateDTO roomRateDTO, RatePlanDTO ratePlanDTO){
        BigDecimal totalAmountBeforeTax = calcAmountBeforeTax(roomRateDTO);
        ChargeItemDTO chargeItemDTO = ratePlanDTO.getTaxesList().get(0);

        int los = DateTimeUtils.calcLOS(chargeItemDTO.getStartDate(), chargeItemDTO.getEndDate()) + 1;

        if (chargeItemDTO.getType() == ChargeItemDTO.ChargeType.PERCENT) {
            return totalAmountBeforeTax.multiply(BigDecimal.valueOf(chargeItemDTO.getValue()));
        } else if (chargeItemDTO.getType() == ChargeItemDTO.ChargeType.FIXED) {
            return BigDecimal.valueOf(chargeItemDTO.getValue()).multiply(BigDecimal.valueOf(los));
        }
        return BigDecimal.ZERO;
    }
}
